package com.itheima.manager.dao;

import com.itheima.manager.domain.Student;

/**
 * @Author Chris
 * @Date 2020/6/15 22:02
 */
public class StudentDao implements BaseDao<Student> {
    public static Student [] stus=new Student[5];
    static {
        stus[0]=new Student("小红","001","23","1997-12-13");
        stus[1]=new Student("小兰","002","25","1997-12-13");
        stus[2]=new Student("小粉","003","22","1997-12-13");

    }
    @Override
    public boolean addStu(Student student) {
        boolean flag=false;
        for (int i = 0; i < stus.length; i++) {
            if(stus[i]==null){
                stus[i]=student;
                flag=true;
                break;
            }

        }
        return flag;
    }

    @Override
    public void deleteStu(String ID) {
        int index=getIndex(ID);
        stus[index]=null;
    }

    @Override
    public Student[] findAllStu() {
        return stus;
    }


    @Override
    public void updateStu(Student student) {
        int index = getIndex(student.getID());
        stus[index]=student;
    }

    public int getIndex(String ID){
        int index=-1;
        for (int i = 0; i < stus.length; i++) {
            if(stus[i]!=null&&stus[i].getID().equals(ID)){
                index=i;
                break;
            }

        }
        return index;
    }
}
